package com.keur.money.ui.main.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayout
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.adapter.MullaRecyclerViewAdapter
import com.keur.money.base.adapter.MullaViewHolderHelper
import com.keur.money.base.utils.ImageUtil
import com.keur.money.config.KeurConfigs
import com.keur.money.databinding.ItemMullaProductTagBinding
import org.json.JSONObject
import java.math.BigDecimal

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/26 16:42
 * @Annotation：产品列表适配器
 */

class KeurProductAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when(model.optBoolean("isApply",false)){
            true -> {//可申请列表
                R.layout.item_mulla_product_apply
            }
            else -> {//已申请列表
                R.layout.item_mulla_product_applied
            }
        }
    }

    override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        helper.setItemChildClickListener(R.id.btnApply) // 申请
    }

    @SuppressLint("SetTextI18n")
    override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.textName,model.optString("name"))
        val imgLogo = helper.getImageView(R.id.imgLogo)
        ImageUtil.instance?.loadImageUrl(mContext,imgLogo,model.optString("logo"), R.mipmap.icon_holder)

        val showAmount = BigDecimal(model.optString("amount")).stripTrailingZeros().toPlainString()
        val textAmount = helper.getTextView(R.id.textAmount)
        textAmount.text = model.optString("currency") + " " + showAmount
        textAmount.setTextColor(Color.parseColor("#ffffac25"))

        when(model.optBoolean("isApply",false)){
            true -> {//可申请列表
                showMullaApplyViews(helper,position,model)
            }
            else -> {//已申请列表
                showMullaAppliedViews(helper,position,model)
            }
        }
    }

    /**
     * 可申请列表
     */
    @SuppressLint("SetTextI18n")
    private fun showMullaApplyViews(helper: MullaViewHolderHelper, position: Int, model: JSONObject){
        helper.setVisibility(R.id.textMinAmount,View.GONE)
        val tags = model.optJSONArray("tags")
        if (model.optBoolean("showSignRise",false)){
            tags?.put(MyApp.app.getString(R.string.product_tag_sign_rise))
            helper.setVisibility(R.id.textMinAmount,View.VISIBLE)
            val showMinAmount = BigDecimal(model.optString("amount")).stripTrailingZeros().toPlainString()
            helper.setText(R.id.textMinAmount,showMinAmount)

            val signRiseAmount = BigDecimal(model.optString("signRiseAmount")).stripTrailingZeros().toPlainString()
            val textAmount = helper.getTextView(R.id.textAmount)
            textAmount.text = model.optString("currency") + " " + signRiseAmount
            textAmount.setTextColor(Color.parseColor("#FFD15A00"))
        }

        // 使用BigDecimal来保存数值
        val bigDecimalValue = BigDecimal(model.optString("interestRate")).multiply(BigDecimal("100"))
        val showRate = bigDecimalValue.stripTrailingZeros().toPlainString()
        helper.setText(R.id.textInterestRate,"$showRate%")

        //产品标签
        val flexboxLayout = helper.getView<FlexboxLayout>(R.id.flexboxLayout)
        flexboxLayout.removeAllViews()
        if (tags != null) {
            for (i in 0..< tags.length()){
                val tag = tags.optString(i)
                val itemBinding = ItemMullaProductTagBinding.inflate(LayoutInflater.from(mContext))
                itemBinding.textTag.text = tag
                when(i){
                    //1、主产品：黄色+紫色...紫色
                    //2、子产品：黄色+红色...红色
                    0 -> {
                        itemBinding.itemLayout.setBackgroundResource(R.drawable.mulla_circle_fill_ffffac25)
                        itemBinding.imgTag.setImageResource(R.mipmap.mulla_product_tag_high_icon)
                    }
                    else -> {
                        //主产品
                        if (model.optString("product") == KeurConfigs.KEUR_APPNAME){
                            //签到提额标签
                            if (tag == MyApp.app.getString(R.string.product_tag_sign_rise)){
                                itemBinding.itemLayout.setBackgroundResource(R.drawable.mulla_circle_fill_ffd15a00)
                                itemBinding.imgTag.setImageResource(R.mipmap.mulla_product_tag_sign_rise_icon)
                            } else {
                                itemBinding.itemLayout.setBackgroundResource(R.drawable.mulla_circle_fill_theme)
                                itemBinding.imgTag.setImageResource(R.mipmap.mulla_product_tag_flexible_icon)
                            }
                        } else {
                            itemBinding.itemLayout.setBackgroundResource(R.drawable.mulla_circle_fill_ffff6135)
                            itemBinding.imgTag.setImageResource(R.mipmap.mulla_product_tag_news_icon)
                        }
                    }
                }
                flexboxLayout.addView(itemBinding.root)
            }
        }

        val btnApply = helper.getTextView(R.id.btnApply)
        if (model.optString("activeFlag") == "YES"){
            btnApply.setBackgroundResource(R.drawable.mulla_btn_pressed_12)
            helper.setVisibility(R.id.unlockLayout, View.GONE)
            if (model.optString("applyStatus").endsWith("DENIED")){//被拒绝
                btnApply.text = MyApp.app.getString(R.string.btn_rejected)
            } else {
                btnApply.text = MyApp.app.getString(R.string.btn_apply)
            }
        } else {
            btnApply.setBackgroundResource(R.drawable.mulla_radius_fill_ffc2c2c2_12)
            btnApply.text = MyApp.app.getString(R.string.btn_apply)
            helper.setVisibility(R.id.unlockLayout, View.VISIBLE)
        }
    }

    /**
     * 已申请列表
     */
    private fun showMullaAppliedViews(helper: MullaViewHolderHelper, position: Int, model: JSONObject){
        helper.setText(R.id.textTerm,String.format(MyApp.app.getString(R.string.home_product_term_value),model.optString("days")))

        val imgStatusIcon = helper.getImageView(R.id.imgStatusIcon)
        val textStatus = helper.getTextView(R.id.textStatus)
        val textRepayAfter = helper.getTextView(R.id.textRepayAfter)

        // TODO: 此处不区分状态，统一显示In Review
        imgStatusIcon.setImageResource(R.mipmap.mulla_product_audit_icon)
        textStatus.text = MyApp.app.getString(R.string.home_status_audit)
        textStatus.setTextColor(Color.parseColor("#FF8027FF"))
        textRepayAfter.visibility = View.GONE

        val status = model.optString("status")
        when(status){
            "PENDING" -> {//处理中
            }
            "REPAYMENT" -> {//待还款
            }
            else -> {
            }
        }
    }
}